import 'dart:typed_data';

import 'package:binary/binary.dart';
import 'package:inmotion_protocol/util/byte_util.dart';

class TirePressureSensor {
    String id = "";
    String signalLevel = '';

    @override
    String toString() {
        return 'TirePressureSensor{id: $id, signalLevel: $signalLevel}';
    }

    static int length() {
        return 4; // 3 bytes for id + 1 byte for signalLevel
    }

    TirePressureSensor.fromBytes(Uint8ClampedList bytes) {
        if (bytes.length < 4) {
            throw Exception('Invalid TirePressureSensor param! (len = ${bytes.length})');
        }
        Uint32 data = ByteUtil.bytes2Uint32Le(bytes);

        id = (data.value & 0xFFFFFF).toRadixString(16).padRight(6, '0'); // 提取低 24 位并转为16进制字符串

        // 将1、2字符和5、6字符调换位置
        id = '${id.substring(4, 6)}${id.substring(2, 4)}${id.substring(0, 2)}';

        signalLevel = ((data.value >> 24) & 0xFF).toString(); // 提取高 8 位并转为字符串

    }

}